import {storage} from "@/zdpui/util/storage.js";
import {useRouter} from "vue-router";

export const useUserInfo = () => {
    const router = useRouter()

    // 存储用户的token
    const setToken = token => {
        storage.set("token", token)
    }
    // 存储用户的id
    const setUserId = id => {
        storage.set("userId", id)
    }
    // 存储用户的用户名
    const setUsername = username => {
        storage.set("username", username)
    }
    // 获取用户token
    const getToken = () => {
        return storage.get("token")
    }
    // 获取用户id
    const getUserId = () => {
        return storage.get("userId")
    }
    // 获取用户名
    const getUsername = () => {
        return storage.get("username")
    }
    // 注销方法
    const onLogout = () => {
        storage.clear()
        router.push("/login")
    }

    // 暴露方法
    return {
        setToken,
        setUserId,
        setUsername,
        getToken,
        getUserId,
        getUsername,
        onLogout,
    }
}